/// @Classname : HcAvatarCell
/// @Author :
/// @Time : 2024-03-28 16:57
/// @Description : //构成 头像的单元格

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:hc_ui/config/hc_customer_config.dart';
import 'package:hc_ui/theme/hc_avatar_theme.dart';
import 'package:hc_ui/theme/hc_cell_theme.dart';
import 'package:hc_ui/widget/avatar/hc_avatar.dart';

class HcAvatarCell extends StatelessWidget {
  final EdgeInsets? padding;
  final EdgeInsets? margin;
  final BorderRadius? borderRadius;
  final Color? backgroundColor;
  final String? title;
  final String? subtitle;
  final double avatarSize;
  final double? titleSpacing;
  final double? titleFontSize;
  final double? subtitleFontSize;
  final double textHeight;
  final HcCellTheme? themeData;
  final HcAvatarTheme? avatarTheme;
  final String? avatarName;
  final Widget? avatarChild;
  final Widget? trailing;

  const HcAvatarCell(
      {super.key,
      this.title,
      this.subtitle,
      this.avatarSize = 36,
      this.titleFontSize,
      this.subtitleFontSize,
      this.textHeight = 1.4,
      this.padding,
      this.margin = const EdgeInsets.all(1),
      this.borderRadius,
      this.backgroundColor,
      this.titleSpacing,
      this.themeData,
      this.avatarTheme,
      this.avatarName,
      this.avatarChild,
      this.trailing});

  @override
  Widget build(BuildContext context) {
    //以vertical宽度计 基础文字大小为14,副标题为12
    HcCellTheme cellTheme = themeData ?? HcCellTheme();
    cellTheme = HcCustomerConfig().allTheme.cellTheme.merge(cellTheme.merge(
        HcCellTheme(
            backgroundColor: backgroundColor,
            borderRadius: borderRadius,
            margin: margin,
            cellListPadding: padding ??
                EdgeInsets.symmetric(
                    vertical: cellTheme.commonTheme.vSpacingPage,
                    horizontal: cellTheme.commonTheme.hSpacingPage))));
    double baseTextSize = cellTheme.commonTheme.fontSizeBase;
    double baseSubtitleSize = cellTheme.commonTheme.fontSizeHint;
    double totalHeight = max(cellTheme.cellListPadding.vertical,
        min(avatarSize, cellTheme.cellListPadding.vertical * 1.5));
    double scale =
        (min(avatarSize, totalHeight) - cellTheme.cellListPadding.vertical) /
            (cellTheme.cellListPadding.vertical / 4);
    double finalTitleFontSize = baseTextSize + scale * (scale > 0 ? 2 : 1);
    double finalSubtitleFontSize = baseSubtitleSize + scale * 2;
    if (scale > 0) {
      finalSubtitleFontSize =
          ((totalHeight - (finalTitleFontSize * textHeight).ceil()) /
                  textHeight)
              .floorToDouble();
      finalSubtitleFontSize = finalSubtitleFontSize - finalSubtitleFontSize % 2;
    }
    int realHeight =
        (finalSubtitleFontSize * textHeight + finalTitleFontSize * textHeight)
            .ceil();
    double spacing = (totalHeight != realHeight) ? 2 : 0;

    return Container(
      margin: cellTheme.margin,
      decoration: BoxDecoration(
        border: cellTheme.border,
        borderRadius: cellTheme.borderRadius,
        color: cellTheme.backgroundColor,
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Padding(
            padding: cellTheme.cellListPadding,
            child: HcAvatar(
              size: avatarSize,
              name: avatarName,
              themeData: avatarTheme,
              child: avatarChild,
            ),
          ),
          Expanded(
              child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              if (title != null)
                Text(
                  title!,
                  style: TextStyle(
                      fontSize: titleFontSize ?? finalTitleFontSize,
                      fontWeight: FontWeight.w400,
                      height: textHeight),
                ),
              if (subtitle != null)
                Padding(
                  padding: EdgeInsets.only(top: titleSpacing ?? spacing),
                  child: Text(
                    subtitle!,
                    style: TextStyle(
                        fontSize: subtitleFontSize ?? finalSubtitleFontSize,
                        fontWeight: FontWeight.w400,
                        color: Colors.grey,
                        height: textHeight),
                  ),
                )
            ],
          )),
          if (trailing != null)
            Container(
              margin: cellTheme.trailingMargin,
              child: IconTheme(
                data: IconThemeData(
                    size: cellTheme.trailingSize,
                    color: cellTheme.trailingColor),
                child: trailing!,
              ),
            )
        ],
      ),
    );
  }
}
